﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerOneSpawnManager : MonoBehaviour
{
    public int attackCombo;             //连发几个
    public float attackComboCd;         //连发间隔
    public int attackNum;               //散射几个
    public float attackCd;              //攻击间隔
    public float attackSpeed;           //子弹飞行速度
    public float attackAngle;           //出生角度
    public int attackDmg;               //攻击力

    public PlayerOne master;
    public int currentCombo;

    public void Init(PlayerOne master, int attackCombo, float attackComboCd, int attackNum, float attackCd, float attackSpeed, float attackAngle, int attackDmg)
    {
        this.master = master;
        this.attackCombo = attackCombo;
        this.attackComboCd = attackComboCd;
        this.attackNum = attackNum;
        this.attackCd = attackCd;
        this.attackSpeed = attackSpeed;
        this.attackAngle = attackAngle * Mathf.Deg2Rad;
        this.attackDmg = attackDmg;
        
        Execute();
    }

    public void Execute()
    {
        for (int i = 0; i < attackCombo; i++)
        {
            Invoke("Fire", i * attackComboCd);
            if (i == 0)
            {
                Invoke("SelfDestroy", i * attackComboCd + 1f);
            }

            //坑了
            /*
            if (i > 0)
            {
                Timer timer = new Timer(i * attackComboCd * 1000);
                timer.AutoReset = false;
                timer.Enabled = true;
                timer.Elapsed += new System.Timers.ElapsedEventHandler(Fire);
            }
            else
            {
                Fire(this, null);
            }
            */
        }
    }

    private void Fire()
    {
        for (int i = 0; i < attackNum; i++)
        {
            Vector2 __projectVelocity;
            
            float __projectDir = 0f;

            if (attackNum >= 2)
            {
                __projectDir = -attackAngle + attackAngle * 2 * (i / ((float)attackNum - 1.0f));
            }

            __projectVelocity = new Vector2(attackSpeed * Mathf.Cos(__projectDir), attackSpeed * Mathf.Sin(__projectDir));
            master.SpawnProject(__projectVelocity, attackDmg);
        }
    }

    private void SelfDestroy()
    {
        GameObject.Destroy(this.transform.gameObject);
    }
}
